% 这是nwafu(西北农林科技大学)边栏主题的外部主题.
% 版权(c) 2015-2016, 信息工程学院 耿楠<nangeng@nwafu.edu.cn>
%
% 本主题基于丹麦奥尔堡大学（Aalborg University）的AAU Beamer主题
% 有关这一主题的详情请参考：http://kom.aau.dk/~jkn/latex/latex.php
%
% 这是一个免费代码：发布和修改该代码，请遵守最新版的自由软件联盟发布
% 的GNU通用公共协议
%
% 期望这一代码对你有用，但值得注意的是，我不对任何后续问题进行维护，
% 请参考<http://www.gnu.org/licenses/>查看最新的协议内容。

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerouterthemenwafusidebar}[2019/06/14 v2.0.00 The nwafu Sidebar Beamer Theme]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 载入需要的宏包
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{tikz}
\usetikzlibrary{external}
\RequirePackage{calc}
%\RequirePackage[font=Helv]{tdclock}% 时钟宏包，由于原tdclock用XeLaTeX编译时会有垂直方向遮挡，对其参数进行了调整，请使用当前目录下的tdclock.sty宏包
%, timeinterval=59
%[font=Times,timeinterval=10, timeduration=2.0, timedeath=0, fillcolorwarningsecond=white!60!yellow,
%timewarningfirst=50,timewarningsecond=80,resetatpages=2]
%\initclock

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 主题选项和定义
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 主题选项
\newlength{\beamer@width}
\setlength{\beamer@width}{2cm}
\DeclareOptionBeamer{width}{\beamer@width=#1}
\DeclareOptionBeamer{left}{\def\beamer@sidebarside{left}}
\DeclareOptionBeamer{right}{\def\beamer@sidebarside{right}}
\DeclareOptionBeamer{hideothersubsections}[]{\beamer@nav@subsectionstyle{show/show/hide}}
\DeclareOptionBeamer{hideallsubsections}[]{\beamer@nav@subsectionstyle{hide}}
\DeclareOptionBeamer{hidetitle}{\def\beamer@hidetitle{true}}
\DeclareOptionBeamer{hideauthor}{\def\beamer@hideauthor{true}}
\DeclareOptionBeamer{hideinstitute}{\def\beamer@hideinst{true}}
\DeclareOptionBeamer{shownavsym}{\def\beamer@shownavsym{true}}
\ExecuteOptionsBeamer{left}
\ProcessOptionsBeamer

% 设置页眉高度为帧标题高度的1.3倍
\newlength{\beamer@height}
\usebeamerfont{frametitle} % 使用帧标题字体
\setlength{\beamer@height}{1.3\baselineskip} 
\reset@font % 恢复字体
\def\beamer@lefttext{left} %为了后面的比较
% 分割主文本区域和边栏的分割条的宽度
\newlength{\beamer@barwidth}
\setlength{\beamer@barwidth}{2\beamer@height/40} % 分割条宽度取决于页眉的高度(比例系数是20,于建涛老师建议窄点好，2015.07.09)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% beamer特殊选项
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>% 参考前四个模式(beamer,handout,second and trans).也就是说，除article外的所有模式
{
  \ifbeamercolorempty[fg]{nwafusidebar}{%
    % 如果没有定义边栏颜色主题，则定义(主要是因为未载入nwafusidebar颜色主题)
    \setbeamercolor{nwafusidebar}{use={structure,palette sidebar primary},fg=palette sidebar primary.fg,bg=structure.fg}%
  }{%
    % nwafu页眉已定义，什么都不用处理
  }
  \setbeamersize{sidebar width \beamer@sidebarside=\beamer@width}%
  \setbeamersize{text margin left=0.5cm,text margin right=0.5cm}%
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % 模板
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  % 页眉线
  \defbeamertemplate*{headline}{nwafusidebar theme}{%
    \nwafuheaderbackground%
  }%
  
  % 帧标题
  \defbeamertemplate*{frametitle}{nwafusidebar theme}{%
    \ifx\beamer@sidebarside\beamer@lefttext% 边栏在左边
      \hfill%
    \fi%
    \begin{minipage}[c][\beamer@height][c]{\textwidth-\beamer@height/2}%
      {\usebeamercolor[fg]{framesubtitle}\usebeamerfont{frametitle}\insertframesubtitle\hfill\usebeamercolor[fg]{frametitle}\usebeamerfont{frametitle}|\insertframetitle\par}%
    \end{minipage}%
    \ifx\beamer@sidebarside\beamer@righttext% 边栏在右边
      \hfill%
    \fi%
  }
  
  % 边栏
  \defbeamertemplate*{sidebar \beamer@sidebarside}{nwafusidebar theme}{%
    \nwafusidebarbackground%
    \vspace{\beamer@height}%
    \vspace{2\baselineskip}\par%
    \ifx\beamer@hidetitle\undefined% 短标题
      \vspace{-\baselineskip}%
      \hspace{0.05\beamer@width}%
      {% 
        \usebeamercolor[fg]{title in sidebar}%
        \usebeamerfont{title in sidebar}%
        \insertshorttitle[width={0.9\beamer@width},center,respectlinebreaks]%
      }%
      \hspace{0.05\beamer@width}%
      \vspace{2\baselineskip}\par%
    \fi%
    \ifx\beamer@hideauthor\undefined% 作者缩写
      \vspace{-\baselineskip}%
      \hspace{0.05\beamer@width}%
      {%
        \usebeamercolor[fg]{author in sidebar}%
        \usebeamerfont{author in sidebar}%
        \insertshortauthor[width={0.9\beamer@width},center,respectlinebreaks]%
      }%
      \hspace{0.05\beamer@width}%
      \vspace{\baselineskip}\par%
    \fi%
    \insertverticalnavigation{1.0\beamer@width}%
    \strut\vfill%
    \ifx\beamer@hideinst\undefined% 单位缩写
      \vbox{\hspace{0.05\beamer@width}%
      {%
        \usebeamercolor[fg]{title in sidebar}%
        \usebeamerfont{subtitle in sidebar}%
        \insertshortinstitute[width={0.9\beamer@width},center,respectlinebreaks]%
      }%
      \hspace{0.05\beamer@width}%
      \vspace*{\baselineskip}%
      }%
    \fi%
    \ifx\beamer@shownavsym\undefined% 导航符号
      % 什么都不做
    \else%
      % 如果边栏在右边，插入导航符号
      \ifx\beamer@sidebarside\beamer@lefttext % 边栏在左边
        % 什么都不做
      \else%
        \vspace*{-2\baselineskip}%
        \llap{\usebeamertemplate***{navigation symbols}\hskip0.4cm}%
        \vskip2pt%
      \fi%
    \fi%
  }%

% 当边栏在左边时，默认主题将logo放置在右下角（边栏在右边时不存在这一问题），在此删除这个logo.
  \ifx\beamer@sidebarside\beamer@lefttext % 边栏在左边
    \defbeamertemplate*{sidebar right}{nwafusidebar theme}{%
      % 删除logo，插入导航符号
      \ifx\beamer@shownavsym\undefined% 插入导航符号
        %什么都不做
      \else % 插入导航符号
        \vfill%
        \llap{\usebeamertemplate***{navigation symbols}\hskip0.4cm}%
        \vskip2pt%
      \fi%
    }%
  \fi%
  
  % 边栏中的当前section
  \defbeamertemplate*{section in sidebar}{nwafusidebar theme}{%
    \vbox{%
      \vskip1ex% 插入一个新section时，增加一个额外的距离
      \sidebarnavitem{4pt}{section in sidebar}{%
        \sidebarcurframe{\insertframenumber}%
        \insertsectionhead%
      }%
    }%
  }%

  % 除了当前section外的其它section
  \defbeamertemplate*{section in sidebar shaded}{nwafusidebar theme}{%
    \vbox{%
      \vskip1ex% 插入一个新section时，增加一个额外的距离
      \sidebarnavitem{4pt}{section in sidebar shaded}{\insertsectionhead}%
    }%
  }%

  % 边栏中的当前subsection
  \defbeamertemplate*{subsection in sidebar}{nwafusidebar theme}{%
    \sidebarnavitem{6pt}{subsection in sidebar}{%
      \sidebarcurframe{\insertframenumber}%
      \insertsubsectionhead%
    }%
  }%

  % 除了当前subsection外的其它subsection
  \defbeamertemplate*{subsection in sidebar shaded}{nwafusidebar theme}{%
    \sidebarnavitem{6pt}{subsection in sidebar shaded}{\insertsubsectionhead}%
  }%

  % 边栏中的当前subsubsection
  \defbeamertemplate*{subsubsection in sidebar}{nwafusidebar theme}{%
    \sidebarnavitem{8pt}{subsubsection in sidebar}{%
      \sidebarcurframe{\insertframenumber}%
      \insertsubsubsectionhead%
    }% 
  }%

  % 除了当前subsubsection外的其它subsubsection
  \defbeamertemplate*{subsubsection in sidebar shaded}{nwafusidebar theme}{%
    \sidebarnavitem{8pt}{subsubsection in sidebar shaded}{\insertsubsubsectionhead}% 
  }%

  \ifx\beamer@shownavsym\undefined% 插入导航符号
    \setbeamertemplate{navigation symbols}{%
      % 禁用导航符号
    }%
  \fi%

}% 结束beamer特殊选项设置

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 主题中要用到的宏
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 漂亮的页眉背景
\newcommand{\nwafuheaderbackground}[0]{%
  \tikzexternaldisable%
  \begin{tikzpicture}[overlay]%
    \ifx\beamer@sidebarside\beamer@lefttext% 边栏在左边
      \coordinate (UC) at (\paperwidth,0); % 右上角
      \coordinate (LC) at (0,-\paperheight); % 左下角
      \coordinate (BS) at (\paperwidth,-\beamer@height); % 分割条的起始坐标
      \coordinate (BE) at (\beamer@width+\beamer@barwidth/2,-\paperheight); % 分割条的终止坐标
      \coordinate (UW) at (\beamer@width/2 - \beamer@height/4 - 0.5pt, 0); % 边栏顶边水平中点
    \else% 边栏在右边
      \coordinate (UC) at (0,0); % 左上角
      \coordinate (LC) at (\paperwidth,-\paperheight); % 右下角
      \coordinate (BS) at (0,-\beamer@height); % 分割条起始坐标
      \coordinate (BE) at (\paperwidth-\beamer@width-\beamer@barwidth/2,-\paperheight); % 分割条终止坐标
      \coordinate (UW) at (\paperwidth-\beamer@width/2+\beamer@height/4 - 1pt, 0); % 边栏顶边水平中点
    \fi%
    \coordinate (IS) at (BS -| BE); % 求过BS的水平线与过BE的垂直线的交点
    \coordinate (logopos) at ([yshift=\beamer@height/2] IS);% logo的坐标
    \coordinate (tdclockpos) at (logopos -| UW);% 时钟中心坐标
    % 绘制页眉背景、圆和logo
    {\usebeamercolor{frametitle}%
      \usebeamercolor{nwafusidebar}%
      \draw[draw=none,fill=frametitle.bg] (UC) rectangle (IS -| LC);% 绘制标题栏背景
      \fill[fill=white] ([yshift=\beamer@height/2] IS) circle (1.0*\beamer@height/2);% 绘制圆形logo背景
      \node at (logopos){\includegraphics[width=0.95\beamer@height]{nwafulogo/nwafu-logo-circle}};% 载入圆形logo
      %\node[anchor=center, frametitle.fg] at (tdclockpos) {\factorclockfont{1.7}\hhmm\tdtime};% 布置时钟
    }%
  \end{tikzpicture}%
  \tikzexternalenable%
}%

% 漂亮的边栏背景
\newcommand{\nwafusidebarbackground}[0]{%
  \tikzexternaldisable
  \begin{tikzpicture}[remember picture,overlay]%
    \ifx\beamer@sidebarside\beamer@lefttext% 边栏在左边
      \coordinate (BS) at (\paperwidth,-\beamer@height); % 分割条的起始坐标
      \coordinate (BE) at (\beamer@width+\beamer@barwidth/2,-\paperheight); % 分割条的终止坐标
    \else% 边栏在右边
      \coordinate (BS) at (-\paperwidth+\beamer@width,-\beamer@height); % 分割条的起始坐标
      \coordinate (BE) at (-\beamer@barwidth/2,-\paperheight); % 分割条的终止坐标
    \fi%
    \coordinate (IS) at (BS -| BE); % 求过BS的水平线与过BE的垂直线的交点
    % 绘制粗线
    {\usebeamercolor{nwafusidebar}%
      \draw[color=fg,line width=\beamer@barwidth] (IS) -- (BE);%
    }%
    % 绘制带圈的帧总数数字
    {\usebeamercolor{palette sidebar primary}%
      \usebeamercolor{nwafusidebar}%
      \usebeamerfont{subsection in sidebar}%
      \node[fill=palette sidebar primary.bg,draw=nwafusidebar.fg,circle,very thick,minimum width=4mm] at ([yshift=2mm] BE) {{\usebeamercolor[fg]{subsection in sidebar}\inserttotalframenumber}};%
    }%
  \end{tikzpicture}%
  \tikzexternalenable%
}

% 边栏导航条目
\newcommand{\sidebarnavitem}[3]{%
  \begin{beamercolorbox}[wd=\beamer@width,leftskip=#1,rightskip=1ex plus1fil,vmode]{#2}%
    %\vbox{}% 插入空白线
    #3\par%
    \vbox{}% 插入空白线
    \vskip-1.5ex%
  \end{beamercolorbox}%
}%

% 当前帧数
\newcommand{\sidebarcurframe}[1]{%
  \tikzexternaldisable%
  \begin{tikzpicture}[remember picture,overlay]%
  \coordinate (CS) at (0,0.8ex); % 当前section标记的坐标
  \coordinate (CF) at (IS |- CS); % 当前帧数的坐标
  {%
    \usebeamercolor{palette sidebar primary}%
    \usebeamercolor{nwafusidebar}%
    \draw[color=bg,line width=\beamer@barwidth] (IS) -| (CF);%
    \node[fill=palette sidebar primary.bg,draw=nwafusidebar.fg,circle,minimum width=3.5mm,thick] at (CF) {{\fontsize{4}{5}\selectfont{\usebeamercolor[fg]{subsection in sidebar}#1}}};%
  }%
  \end{tikzpicture}%
  \tikzexternalenable%
}%

\mode<all>

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "main.tex"
%%% End: 
